package cn.com.wangluotongxin.merchants.service.impl;

import cn.com.wangluotongxin.common.constants.CommonConstant;
import cn.com.wangluotongxin.common.exceptions.BadResponseException;
import cn.com.wangluotongxin.common.pojo.TenantUser;
import cn.com.wangluotongxin.merchants.dto.GoodsSizeDto;
import cn.com.wangluotongxin.merchants.form.GoodsSizeForm;
import cn.com.wangluotongxin.merchants.model.MerchantsGoodsSize;
import cn.com.wangluotongxin.merchants.mapper.MerchantsGoodsSizeMapper;
import cn.com.wangluotongxin.merchants.model.constants.YesNo;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsSizeService;
import cn.com.wangluotongxin.merchants.vo.GoodsSizeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商户管理-商品管理-尺码 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-06-20
 */
@Service
public class MerchantsGoodsSizeServiceImpl extends ServiceImpl<MerchantsGoodsSizeMapper, MerchantsGoodsSize> implements IMerchantsGoodsSizeService {

    @Override
    public IPage<GoodsSizeVo> getPage(GoodsSizeDto dto) {
        Page page = Page.of(dto.getPageNumber(), dto.getPageSize());
        return baseMapper.getPage(page, dto);
    }

    @Override
    public List<GoodsSizeVo> getList(String name) {
        return baseMapper.getList(name);
    }

    @Override
    public void add(GoodsSizeForm form, TenantUser tenantUser) {
        if(this.count(new LambdaQueryWrapper<MerchantsGoodsSize>()
                .eq(MerchantsGoodsSize::getSizeName, form.getSizeName())
                .ne(MerchantsGoodsSize::getIsDeleted, CommonConstant.ONE)) > 0) {
            throw new BadResponseException("尺码【" + form.getSizeName() +"】已存在");
        }

        MerchantsGoodsSize goodsSize = new MerchantsGoodsSize();
        BeanUtils.copyProperties(form, goodsSize);
        goodsSize.setCreateUser(tenantUser.getId());
        goodsSize.setCreateName(tenantUser.getUsername());
        this.save(goodsSize);
    }

    @Override
    public void delete(Integer id) {
        this.update(new LambdaUpdateWrapper<MerchantsGoodsSize>()
                .set(MerchantsGoodsSize::getIsDeleted, YesNo.YES.getCode())
                .eq(MerchantsGoodsSize::getId, id));
    }

    @Override
    public void enabled(Integer id, Integer isEnabled) {
        this.update(new LambdaUpdateWrapper<MerchantsGoodsSize>()
                .set(MerchantsGoodsSize::getIsEnabled, isEnabled)
                .eq(MerchantsGoodsSize::getId, id));
    }
}
